package com.bw.paper.mvp.view.Activity;

import android.os.Bundle;
import android.view.View;

import com.bw.paper.R;
import com.bw.paper.entity.LiveCommentEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contact.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.bw.paper.mvp.view.Fragment.Live.FullVideoActivity;
import com.bw.paper.mvp.view.Fragment.Live.adapter.LiveCommentAdapter;
import com.bw.paper.mvp.view.Fragment.Live.adapter.LiveVpAdapter;
import com.bw.paper.utils.LogUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

public class LiveValuesActivity extends BaseActivity<LREPresenter>implements LREContract.ILREView {

    private StandardGSYVideoPlayer live_gsy_player;
    private SlidingTabLayout live_values_tab;
    private ViewPager live_values_vp;
    private LiveVpAdapter vpAdapter;
    private LiveCommentAdapter commentAdapter;
    private long live_id;
    private String live_url;

    @Override
    public View addStatusView() {
        return f(R.id.live_values_root);
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_live_values;
    }

    @Override
    public void initView() {
        live_id = getIntent().getExtras().getLong("live_id");
        live_url = getIntent().getExtras().getString("live_url");
        LogUtils.show("live_url:"+live_url);
        live_gsy_player = f(R.id.live_gsy_player);
        live_values_tab = f(R.id.live_values_tab);
        live_values_vp = f(R.id.live_values_vp);
        vpAdapter = new LiveVpAdapter();
        live_values_vp.setAdapter(vpAdapter);
        live_values_tab.setViewPager(live_values_vp);
        commentAdapter = new LiveCommentAdapter();
        live_gsy_player.setUp(live_url,true,"");
        live_gsy_player.startPlayLogic();
        initListener();
    }

    private void initListener(){
        live_gsy_player.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("live_url",live_url);
                startPage(bundle, FullVideoActivity.class);
            }
        });
    }


    @Override
    public void initData() {
        Map<String,Object>map = new HashMap<>();
        map.put("code", HttpCode.SELLIVECOMMENTCODE);
        map.put("live_id",1);
        p.requestAll(map);
    }

    @Override
    protected void onResume() {
        super.onResume();
        live_gsy_player.getCurrentPlayer().onVideoResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        live_gsy_player.getCurrentPlayer().onVideoPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        live_gsy_player.getCurrentPlayer().release();
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectLiveValuesActivity(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof LiveCommentEntity){
            LiveCommentEntity liveCommentEntity = (LiveCommentEntity) entity;
            vpAdapter.getRootRv().setAdapter(commentAdapter);
            if (liveCommentEntity.getValues() == null)
                return;
            LogUtils.show("LiveValuesActivity:"+liveCommentEntity.getValues().size());
            commentAdapter.setNewInstance(liveCommentEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }
}